<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2019/12/10
 * Time: 11:09
 */

namespace app\admin\controller;


use app\admin\validate\PrivilegeValidate;
use app\common\model\ShopPrivilege as ShopPrivilegeModel;
use app\common\utils\JsonService;
use think\facade\View;



class ShopPrivilege extends Base
{

    /**
     * 添加权限
     * @return mixed
     */
    public function create(){
        if($this->request->isajax()){

            $post = $this->request->post();
            $validate = new PrivilegeValidate;
            if(!$validate->scene('add')->check($post)) {
                return JsonService::error($validate->getError());
            }
            try {
                ShopPrivilegeModel::create($post);

            } catch (\Exception $ex) {
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success();
        }

        $privilegeData = ShopPrivilegeModel::getTree();
        return View::fetch('create',['privilegeData'=>$privilegeData]);
    }

    /**
     * 编辑权限
     * @return mixed
     */
    public function update(){
        if($this->request->isAjax()){
            $return = $this->return;
            $post = $this->request->post();

            $validate = new PrivilegeValidate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            try {
                $res = ShopPrivilegeModel::updateOne($post,['id'=>$post['id']]);
            } catch (\Exception $ex) {
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success();
        }

        $id = $this->request->get('id');
        $privilege = $this->findModel($id);
        $privilegeData = ShopPrivilegeModel::getTree();
        return View::fetch('update',[
            'privilege'=>$privilege,
            'privilegeData'=>$privilegeData,
        ]);
    }

    /**
     * 权限列表
     * @return mixed
     */
    public function index(){
        $privilegeData = ShopPrivilegeModel::getTree();
        return View::fetch('index',['privilegeData'=>$privilegeData]);
    }

    /*
     * 删除权限
     * */
    public function delete(){
        if($this->request->isAjax()){
            $id = $this->request->post('id');
            try{
                $model = $this->findModel($id);
                $model->delete();
            }catch (\Exception $ex){
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success('删除成功');
        }
    }

    /**
     * 得到模型
     *
     * @param       $id
     * @param array $where
     *
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }

        return ShopPrivilegeModel::findModel($id, $where);
    }
}